<?php

namespace App\Http\Controllers;

use App\Models\Component;
use Illuminate\Http\Request;

class ComponentController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function get_all(Request $request)
    {
        // $components = Component::all();
        return response()->json([
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'components' => [
                    [
                        'title' => 'child组件',
                        'icon' => 'test',
                        'type' => 'component',
                        'name' => 'Juemiao\Child\Child',
                        'property' => [
                            [
                                'label' => '字号',
                                'prop' => 'fontSize',
                                'type' => 'number',
                                'value' => 15,
                            ],
                        ]
                    ],
                    [
                        'title' => 'Swiper',
                        'icon' => 'swiper',
                        'type' => 'component',
                        'name' => 'Juemiao\Swiper\Swiper', 
                        'property' => [
                            [
                                'label' => 'items',
                                'prop' => 'items',
                                'type' => 'array',
                                'value' => [],
                            ]
                        ]
                    ],
                    [
                        'title' => '导航',
                        'icon' => 'swiper',
                        'type' => 'component',
                        'name' => 'Juemiao\NavHeader\NavHeader', 
                    ]
                ]
            ],
        ]);
    }

    public function get_view(Request $request)
    {
        // 获取 post数据
        $data = $request->post();
        $app = app();
        $component = $app->make($data['name']);
        return response()->json([
            'code' => 0,
            'msg' => 'success',
            'data' => [
                ...$this->extractJS($component->getView()->render()),
            ],
        ]);
    }
}
